

var audiolet = new Audiolet(44100,2,512);

var pianoKey = cc.Node.extend({
    keyboardKey:-1,
    latinNote:"A",
    note:null,
    sine:null,
    
    
    ctor:function ( _pianoKeyDef ) {
        var selfPointer = this;
        this._super();
        
        if( _pianoKeyDef )
        {
            this.keyboardKey = _pianoKeyDef.keyboardKey;
            this.latinNote = _pianoKeyDef.latinNote;
        
            this.note = Note.fromLatin( this.latinNote );
        }
        
        
        var img = cc.CustomSprite.create("res/piano_key_up.png");
        this.addChild( img, 0, pianoKey.UP_TAG );
        
        img = cc.CustomSprite.create("res/piano_key_down.png");
        img.setVisible(false);
        this.addChild( img, 1, pianoKey.DOWN_TAG );
        
        
        var text = this.note.latin();
        var textFr = text;
        
        var convertToFrench = true;
        if(convertToFrench)
        {
            if(textFr == "A")
                textFr = "la";
            else if(textFr == "B")
                textFr = "si";
            else if(textFr == "C")
                textFr = "do";
            else if(textFr == "D")
                textFr = "re";
            else if(textFr == "E")
                textFr = "mi";
            else if(textFr == "F")
                textFr = "fa";
            else if(textFr == "G")
                textFr = "sol";

        }
        
        
        var label = cc.LabelTTF.create( text , "Arial", 15);
        label.setPosition(cc.p(17,35));
        label.setColor( cc.c3b(0,0,0) );
        this.addChild(label,2);
        
        label = cc.LabelTTF.create( textFr , "Arial", 10);
        label.setPosition(cc.p(18,23));
        label.setColor( cc.c3b(0,0,0) );
        this.addChild(label,2);
    },
    
    onKeyUp:function(e){
        if(e == this.keyboardKey )
        {
            var node = this.getChildByTag( pianoKey.DOWN_TAG );
            node.setVisible(false);

            if( this.sine != null )
            {
                //this.sine.disconnect(audiolet.output);
                this.sine = null;
            }
        }
    },
    onKeyDown:function(e){
        if(e == this.keyboardKey )
        {
            var node = this.getChildByTag( pianoKey.DOWN_TAG );
            node.setVisible(true);
            
            if( this.sine == null )
            {
                console.log("keydwon");
                var note = this.note;
                this.sine = new Synth(audiolet, note.frequency() );
                this.sine.connect(audiolet.output);
            }
        }
    },
});
pianoKey.UP_TAG = 0;
pianoKey.DOWN_TAG = 1;


var piano = cc.Node.extend({ 

    Keys:[],
    
    ctor:function ( _pianoKeyDefList ) {
        var selfPointer = this;
        this._super();
        
        var p = cc.p(0,0);
        for(var i=0; i< _pianoKeyDefList.length; i++)
        {
            this.Keys[i] = new pianoKey( _pianoKeyDefList[i] );
            this.Keys[i].setPosition( p );

            p =  cc.p( p.x + 33, p.y + 0 );
            
            this.addChild( this.Keys[i] );
        }
    },
    
    onKeyUp:function(e){
        for(var i=0; i<this.Keys.length; i++)
        {
            this.Keys[i].onKeyUp(e);
        }
    },
    
    onKeyDown:function(e){
        for(var i=0; i<this.Keys.length; i++)
        {
            this.Keys[i].onKeyDown(e);
        }
    },
});



function _pianoKeyDef (_keyboardKey,_latinNote) {
    this.keyboardKey = _keyboardKey.charCodeAt(0);
    this.latinNote = _latinNote;
};

function pianoKeyDef (_keyboardKey,_latinNote){
    return new _pianoKeyDef(_keyboardKey,_latinNote);
};

var _pianoScene = cc.Layer.extend({
   
    _piano:null,
   
    init:function () {
        var selfPointer = this;
        //////////////////////////////
        // 1. super init first
        this._super();

        this.setTouchEnabled(true);
        this.setKeyboardEnabled(true);
        
        
        var size = cc.Director.getInstance().getWinSize();

        // background

        var background = cc.Sprite.create("res/LightedBackground.png");
        background.setPosition(cc.p(size.width / 2, size.height / 2));
        background.setVisible(true);
        background.setAnchorPoint(cc.p(0.5, 0.5));
        this.addChild(background, 0);

    
        //Menu (back button)
        var items = new Array();
        var p = cc.p( (-size.width/2) + 20 ,
                      (-size.height/2) + 30 );
        items[0] = createButton("Back");
        items[0].setPosition( p );
        items[0].setCallback(this,
            function () {
                myApp.GoToScene( new HelloWorldScene() );
            });
        var myMenu = cc.MenuCustom.create(items);
        this.addChild(myMenu);
        
        this.scheduleUpdate();
        
        
        
        
        var label = cc.LabelTTF.create( "E E E C E G C" , "Arial", 30);
        label.setPosition(cc.p(350,300));
        label.setColor( cc.c3b(0,0,0) );
        this.addChild(label);
        
        
        /*
        mario note
        E,E ,E,C,E,G,G ,C,G,E,A,
        B,A#,G,E,G,A,F ,G,E,C,D,AS
        B,C ,G,E,A,B,A#,G,E,G,A,
        F,G ,E,C,D,B
        */
        
        // start specific init 
        /*
        this._piano = new piano( [
                                    pianoKeyDef("S","E5"),
                                    pianoKeyDef("D","E5"),
                                    pianoKeyDef("F","E5"),
                                    pianoKeyDef("G","C5"),
                                    pianoKeyDef("H","E5"),
                                    pianoKeyDef("J","G5"),
                                    pianoKeyDef("K","C5"),
                                    pianoKeyDef("L","C5"),
                                    ] );
        */
        
        this._piano = new piano( [
                                    pianoKeyDef("A","C5"),
                                    pianoKeyDef("S","D5"),
                                    pianoKeyDef("D","E5"),
                                    pianoKeyDef("F","F5"),
                                    pianoKeyDef("G","G5"),
                                    pianoKeyDef("H","A5"),
                                    pianoKeyDef("J","B5"),
                                    pianoKeyDef("K","C6"),
                                    pianoKeyDef("L","D6"),
                                    ] );
        
        this._piano.setPosition( cc.p(200,100) );
        this.addChild( this._piano );
        
        return true;
    },
    
    update:function (dt) {
    },
    
    // inputs 
    onTouchesBegan:function (pTouch, event) {
    },
    onTouchesMoved:function(pTouch,pEvent){
    },
    onTouchesEnded:function (pTouch,pEvent){
    },
    onKeyUp:function(e){
        this._piano.onKeyUp(e);
    },
    onKeyDown:function(e){
        this._piano.onKeyDown(e);
    },
});


var pianoScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var layer = new _pianoScene();
        layer.init();
        this.addChild(layer);
    }
});


